﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace Rio.Framework.Pages.ModalControls
{

    public class ModalDialogController
    {
        private static ModalDialogController m_oInstance;

        private Popup m_puPopUp = null;
        private ModalDialog m_mdDialog = null;
        private UserControl m_oModalDialogUserControl;
        private event EventHandler<ModalDialogEventArgs> m_eRequestDialogClose;
        private static double m_dApplicationHeight = 0;
        private static double m_dApplicationWidth = 0;

        //private static AutoResetEvent areSyncDemoThread = new AutoResetEvent(false);

        private ModalDialogController(UserControl P_oModalDialogUserControl, EventHandler<ModalDialogEventArgs> p_ehEventHandler = null)
        {
            m_eRequestDialogClose = p_ehEventHandler;
            m_oModalDialogUserControl = P_oModalDialogUserControl;

            m_mdDialog = new ModalDialog()
            {
                Height = m_dApplicationHeight,
                Width = m_dApplicationWidth,
            };
            m_mdDialog.AddControl(m_oModalDialogUserControl);

            m_puPopUp = new Popup()
            {
                Child = m_mdDialog,
                IsOpen = true,
            };

            if (m_oModalDialogUserControl.GetType().Equals(typeof(Message)))
            {
                ( (Message)m_oModalDialogUserControl ).Closed += new EventHandler(ModalDialogController_Closed);
            }
        }

        public static void Launch(UserControl P_oModalDialogUserControl, EventHandler<ModalDialogEventArgs> p_ehEventHandler = null)
        {
            m_dApplicationWidth = Application.Current.Host.Content.ActualWidth;
            m_dApplicationHeight = Application.Current.Host.Content.ActualHeight;

            if (m_oInstance == null)
            {
                m_oInstance = new ModalDialogController(P_oModalDialogUserControl, p_ehEventHandler);

                #region unused code
                //if (m_oInstance.m_oModalDialogUserControl.GetType().Equals(typeof(Message)))
                //{
                //    Monitor.Enter(((Message)m_oInstance.m_oModalDialogUserControl).m_oDialogResult);
                //}
                //Thread L_oThread = new Thread(Block);
                //L_oThread.Start();
                //L_oThread.Join();
                #endregion
            }
        }

        void ModalDialogController_Closed(object sender, EventArgs e)
        {
            Close();
        }

        public static void Close(ModalDialogEventArgs p_ehEventHandler = null)
        {
            if (m_oInstance != null)
            {
                #region unused code
                //if (m_oInstance.m_oModalDialogUserControl.GetType().Equals(typeof(Message)))
                //{
                //    Monitor.Exit(((Message)m_oInstance.m_oModalDialogUserControl).m_oDialogResult);
                //}
                #endregion

                m_oInstance.m_puPopUp.IsOpen = false;
                if (m_oInstance.m_mdDialog != null)
                {
                    m_oInstance.m_mdDialog.ClearControl();
                }
                m_oInstance.m_mdDialog = null;
                m_oInstance.m_puPopUp.Child = null;

                if (m_oInstance.m_eRequestDialogClose != null)
                {
                    m_oInstance.m_eRequestDialogClose(m_oInstance.m_oModalDialogUserControl, p_ehEventHandler);
                }

                #region unused code
                //if (m_oInstance.m_oModalDialogUserControl != null && m_oInstance.m_oModalDialogUserControl.GetType().Equals(typeof(Message)))
                //{
                //    areSyncDemoThread.Set();
                //}
                #endregion

                m_oInstance.m_oModalDialogUserControl = null;
                m_oInstance = null;
            }
        }

        public static void Application_Resized(object sender, SizeChangedEventArgs e)
        {
            m_dApplicationHeight = e.NewSize.Height;
            m_dApplicationWidth = e.NewSize.Width;

            if (m_oInstance != null)
            {
                m_oInstance.m_mdDialog.Width = m_dApplicationWidth;
                m_oInstance.m_mdDialog.Height = m_dApplicationHeight;
            }
        }
    }
}